package com.ibiaoke.customer.service;

import com.ibiaoke.common.utils.DateUtils;
import com.ibiaoke.common.utils.RandomUtils;
import com.ibiaoke.customer.consts.TradeStatusConsts;
import com.ibiaoke.customer.consts.TradeTypeConsts;
import com.ibiaoke.customer.dao.TradeRecorderDao;
import com.ibiaoke.customer.entity.TradeRecorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

@Service
public class TradeRecorderServiceImpl implements TradeRecorderService {

    @Autowired
    private TradeRecorderDao tradeRecorderDao;
    @Autowired
    private DepositRecorderService depositRecorderService;
    @Autowired
    private DepositService depositService;

    @Override
    public TradeRecorder createTradeRecorder(Integer customerId, Integer payType, BigDecimal money, Integer tradeType) {
        TradeRecorder recorder = new TradeRecorder();
        recorder.setTradeNo(generateTradeNo());
        recorder.setCustomerId(customerId);
        recorder.setPayType(payType);
        recorder.setTradeType(tradeType);
        recorder.setMonney(money);
        recorder.setStatus(TradeStatusConsts.DEALING);
        tradeRecorderDao.insertSelective(recorder);
        return recorder;
    }

    @Override
    @Transactional
    public int depositChargeSuccess(String tradeNo, Integer payType, String transactionNo) throws ParseException {
        TradeRecorder tradeRecorder = tradeRecorderDao.getTradeRecorder(tradeNo);
        if (tradeRecorder == null || tradeRecorder.getStatus() == TradeStatusConsts.SUCCESS) {
            return 0;
        }
        int count = tradeRecorderDao.updateToSuccess(tradeRecorder.getTradeId(), payType, transactionNo);
        if (count > 0) {
            BigDecimal monney = tradeRecorder.getMonney();
            Integer customerId = tradeRecorder.getCustomerId();
            depositRecorderService.insert(customerId, payType, transactionNo, TradeTypeConsts.CHARGE, monney);
            depositService.depositCharge(customerId, monney);
            //充值1000
//            if (monney.compareTo(new BigDecimal("1000")) >= 0) {
//                for (int i = 0; i < 40; i++) {
//                    couponReceiveService.receiveCouponNonCountLimit(customerId, CouponGroupDef.CHARGE_1000);
//                }
//                //充值500
//            } else if (monney.compareTo(new BigDecimal("500")) >= 0) {
//                for (int i = 0; i < 12; i++) {
//                    couponReceiveService.receiveCouponNonCountLimit(customerId, CouponGroupDef.CHARGE_500);
//                }
//                //充值100
//            } else if (monney.compareTo(new BigDecimal("100")) >= 0) {
//                for (int i = 0; i < 2; i++) {
//                    couponReceiveService.receiveCouponNonCountLimit(customerId, CouponGroupDef.CHARGE_100);
//                }
//            }

            //充值500送 4斤S级-塔斯马尼亚车厘子 优惠券
            /*boolean flag = false;
            Coupon coupon = couponDao.getCouponById(2373);
            if(coupon.getTotalCount() > 0){
                flag = true;
            }
            if(flag){
                if (monney.compareTo(new BigDecimal("500")) == 0) {
                    CouponReceive CouponReceive = new CouponReceive();
                    CouponReceive.setUserId(customerId);
                    CouponReceive.setCouponId(2373);
                    CouponReceive.setCouponName(coupon.getCouponName());
                    CouponReceive.setScopeType(coupon.getScopeType());
                    CouponReceive.setCouponPrice(coupon.getCouponPrice());
                    CouponReceive.setFullPrice(coupon.getFullPrice());
                    CouponReceive.setUseBeginTime(new Date());
                    Calendar c = Calendar.getInstance();
                    c.add(Calendar.DATE,coupon.getDurationDay());
                    CouponReceive.setUseEndTime(c.getTime());
                    CouponReceive.setDurationDay(coupon.getDurationDay());
                    CouponReceive.setUseStatus((byte)0);
                    CouponReceive.setReceiveTime(new Date());
                    CouponReceive.setUseType(1);
                    CouponReceive.setCanUseId(407);
                    couponReceiveDao.insert(CouponReceive);
                    if(coupon.getTotalCount()> 0){
                        couponDao.updateTotalCount(coupon,coupon.getTotalCount()-1);
                    }
                    }
                }*/
            }
        return count;
    }

    @Override
    public String generateTradeNo() {
        String dateNo = DateUtils.formatOrderNoDate(new Date());
        return dateNo + RandomUtils.generatorOrderNo();
    }
}
